from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *

import sys
import resources_rc

import rastercompute

from rasterform import RasterDialog

class RasterAlgebra:

  def __init__(self, iface):
    # save reference to the QGIS interface
    self.iface = iface
    return None

  def initGui(self):
      
      # create action that will start plugin configuration
      self.action = QAction(QIcon(":/plugins/rasteralgebra/rastericon.xpm"), "Raster Algebra", self.iface.mainWindow())
      self.action.setWhatsThis("Configuration for test plugin")
      QObject.connect(self.action, SIGNAL("activated()"), self.run)

      # add toolbar button and menu item
      self.iface.addToolBarIcon(self.action)
      self.iface.addPluginToMenu("&Raster Algebra", self.action)
      
      ## no need to... connect to signal renderComplete which is emitted when canvas rendering is done
      ##QObject.connect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)

  def unload(self):
    # remove the plugin menu item and icon
    self.iface.removePluginMenu("&Raster Algebra",self.action)
    self.iface.removeToolBarIcon(self.action)

    # disconnect form signal of the canvas
    #QObject.disconnect(self.iface.mapCanvas(), SIGNAL("renderComplete(QPainter *)"), self.renderTest)


  def setupItems(self,a):
      #
      # add raster layers
      #
      mapCanvas = self.iface.mapCanvas()
      a.ui.layer1.clear()
      a.ui.layer2.clear()
      a.layers=[]
      for layerIndex in range(0,mapCanvas.layerCount()):
          layer=mapCanvas.layer(layerIndex)
          if layer.type() == layer.RasterLayer:
              a.layers.append(layer)
              a.ui.layer1.addItem(layer.name())
              a.ui.layer2.addItem(layer.name())
          

      # add functions and ops
      a.ui.op.addItems(rastercompute.ops())
      a.ui.func1.addItems(rastercompute.funcs())
      a.ui.func2.addItems(rastercompute.funcs())
      
      a.ui.createNewLayer.setChecked(True)

  def run(self):
    
    a=RasterDialog()
    self.setupItems(a)
    a.show()
    result = a.exec_()

    if result != 1:
        # cancel
        return 

    # package up a 'formula' dict and pass it on
    formula = {
        'func1': str(a.ui.func1.currentText()),
        'layer1':a.layers[a.ui.layer1.currentIndex()] ,
        'constant1': str(a.ui.constant1.text()),
        'useLayer1':a.ui.useLayer1.isChecked(),
        'op': str(a.ui.op.currentText()),
        'func2': str(a.ui.func2.currentText()),
        'layer2':a.layers[a.ui.layer2.currentIndex()] ,
        'constant2':str(a.ui.constant2.text()),
        'useLayer2':a.ui.useLayer2.isChecked(),
        }

    rastercompute.createRaster(a.ui.outFileName.text(),formula)

    # now load the layer if that worked...

    if a.ui.createNewLayer.isChecked():
      print "loading layer..."
      fileInfo = QFileInfo(a.ui.outFileName.text())
      baseName = fileInfo.baseName()
      rlayer = QgsRasterLayer(a.ui.outFileName.text(), baseName)

      QgsMapLayerRegistry.instance().addMapLayer(rlayer)

      
    
#  def renderTest(self, painter):
#    # use painter for drawing to map canvas
#    print "Raster Algebra: renderTest called!"

